package org.junyee.demo.security.ec;

import java.nio.charset.StandardCharsets;

import org.springframework.util.Base64Utils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
class TestEc {

    public static void main(String... a) {
        
        // disable pkcs8 ASN.1 put optional [0]
        System.setProperty(EcPem.SECURITY_SUN_EC_PARAM_OPTIONAL_DISABLE, "true");
        // disable SunEC EcParameterSpec provided
        System.setProperty(EcEs.SECURITY_SUN_EC_PARAM_SPEC_DISABLE, "true");
        // enable SunEC DsaUtil.java to convert ES signature
        System.setProperty(EcUtils.SECURITY_SUN_EC_DSA_UTIL_DISABLE, "false");

        log.info("-----------------------------JWK-----------------------------");
        EcKey ecKey = EcKey.createPrivateKeyFromUrlSafeBase64("Jg_XvZqKhb2tf8g7HZnN45UYYdp7jgesFS0YJAqcyEg",
                "AKK-yxZabtVhWO99dw0v2gCpxAMyY2HCGjjOfuSO7YRX", "QZoeD-gpDLvF01-jB4cN878TvTytxoHZwqyQwb74yZo",
                EcEs.ES256);
        byte[] signs = ecKey.signES("123123".getBytes(StandardCharsets.UTF_8));
        log.info(Base64Utils.encodeToString(signs));
        boolean rs = ecKey.verifyES(signs, "123123".getBytes(StandardCharsets.UTF_8));
        log.info(String.valueOf(rs));
        log.info(ecKey.toString());

        log.info("-----------------------------ES128-----------------------------");
        ecKey = EcKey.generate(EcEs.ES128);
        signs = ecKey.signES("123123".getBytes(StandardCharsets.UTF_8));
        log.info(Base64Utils.encodeToString(signs));
        rs = ecKey.verifyES(signs, "123123".getBytes(StandardCharsets.UTF_8));
        log.info(String.valueOf(rs));
        log.info(ecKey.toString());
        log.info("d: {}", ecKey.getBase64URLSafeD());
        log.info("x: {}", ecKey.getBase64URLSafeX());
        log.info("y: {}", ecKey.getBase64URLSafeY());

        log.info("-----------------------------ES256-----------------------------");
        ecKey = EcKey.generate(EcEs.ES256);
        signs = ecKey.signES("123123".getBytes(StandardCharsets.UTF_8));
        log.info(Base64Utils.encodeToString(signs));
        rs = ecKey.verifyES(signs, "123123".getBytes(StandardCharsets.UTF_8));
        log.info(String.valueOf(rs));
        log.info(ecKey.toString());
        log.info("d: {}", ecKey.getBase64URLSafeD());
        log.info("x: {}", ecKey.getBase64URLSafeX());
        log.info("y: {}", ecKey.getBase64URLSafeY());

        log.info("-----------------------------ES384-----------------------------");
        ecKey = EcKey.generate(EcEs.ES384);
        signs = ecKey.signES("123123".getBytes(StandardCharsets.UTF_8));
        log.info(Base64Utils.encodeToString(signs));
        rs = ecKey.verifyES(signs, "123123".getBytes(StandardCharsets.UTF_8));
        log.info(String.valueOf(rs));
        log.info(ecKey.toString());
        log.info("d: {}", ecKey.getBase64URLSafeD());
        log.info("x: {}", ecKey.getBase64URLSafeX());
        log.info("y: {}", ecKey.getBase64URLSafeY());

        log.info("-----------------------------ES512-----------------------------");
        ecKey = EcKey.generate(EcEs.ES512);
        signs = ecKey.signES("123123".getBytes(StandardCharsets.UTF_8));
        log.info(Base64Utils.encodeToString(signs));
        rs = ecKey.verifyES(signs, "123123".getBytes(StandardCharsets.UTF_8));
        log.info(String.valueOf(rs));
        log.info(ecKey.toString());
        log.info("d: {}", ecKey.getBase64URLSafeD());
        log.info("x: {}", ecKey.getBase64URLSafeX());
        log.info("y: {}", ecKey.getBase64URLSafeY());

        // create EcKey from openssl pkcs8 private key pem file
        ecKey = EcKey.createKeyFromPkcs8Pem(ecKey.getEcPem().getPrivatePem());
        signs = ecKey.signES("123123".getBytes(StandardCharsets.UTF_8));
        log.info(Base64Utils.encodeToString(signs));
        rs = ecKey.verifyES(signs, "123123".getBytes(StandardCharsets.UTF_8));
        log.info(String.valueOf(rs));
        log.info("d: {}", ecKey.getBase64URLSafeD());
        log.info("x: {}", ecKey.getBase64URLSafeX());
        log.info("y: {}", ecKey.getBase64URLSafeY());

        // create EcKey from openssl pkcs8 public key pem file
        ecKey = EcKey.createKeyFromPkcs8Pem(ecKey.getEcPem().getPublicPem());
        rs = ecKey.verifyES(signs, "123123".getBytes(StandardCharsets.UTF_8));
        log.info(String.valueOf(rs));
        log.info("d: {}", ecKey.getBase64URLSafeD());
        log.info("x: {}", ecKey.getBase64URLSafeX());
        log.info("y: {}", ecKey.getBase64URLSafeY());
    }
}
